package com.baidu.mpks.common;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;

public class HttpClientManager {
    private int maxTotal = 700;
    private int maxPerRoute = 500;
    private int connectionTimeout = 20000;
    private int soTimeout = 20000;
    private HttpClient httpclient;

    public HttpClientManager() {
    }

    public void init() {
        PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
        cm.setMaxTotal(this.maxTotal);
        cm.setDefaultMaxPerRoute(this.maxPerRoute);
        HttpParams params = new BasicHttpParams();
        DefaultHttpClient.setDefaultHttpParams(params);
        params.setParameter("http.connection.timeout", this.connectionTimeout);
        params.setParameter("http.socket.timeout", this.soTimeout);
        this.httpclient = new DefaultHttpClient(cm, params);
    }

    public HttpClient getHttpclient() {
        return this.httpclient;
    }

    public void destroy() {
        this.httpclient.getConnectionManager().shutdown();
    }

    public int getMaxTotal() {
        return this.maxTotal;
    }

    public void setMaxTotal(int maxTotal) {
        this.maxTotal = maxTotal;
    }

    public int getConnectionTimeout() {
        return this.connectionTimeout;
    }

    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public int getSoTimeout() {
        return this.soTimeout;
    }

    public void setSoTimeout(int soTimeout) {
        this.soTimeout = soTimeout;
    }
}
